package com.hnas.ecard.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.Query;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.util.StringUtil;
import com.hnas.ecard.dao.SaleCardOrderDetailDao;
import com.hnas.ecard.orm.SaleCardOrderDetail;
import com.hnas.ecard.service.dto.DTOSaleCardOrder;
import com.hnas.ecard.service.dto.DTOSaleCardOrderDetail;
import com.hnas.ecard.service.dto.DTOSaleCardRecord;

@Component("saleCardOrderDetailDao")
public class SaleCardOrderDetailDaoImpl extends BaseDaoImpl<SaleCardOrderDetail> implements SaleCardOrderDetailDao {

	public SaleCardOrderDetailDaoImpl() {
		super(SaleCardOrderDetail.class);
	}

	@Override
	public SaleCardOrderDetail saveOrUpdate(DTOSaleCardOrderDetail dto) {
		SaleCardOrderDetail vo = null;
		try {
			if(dto != null){
				String id = dto.getId();
				if(StringUtil.isNotEmpty(id)){
					vo = this.find(id);
				}	
				if(vo == null){//新增
					vo = new SaleCardOrderDetail();
					BeanUtils.copyProperties(dto, vo);
					this.persist(vo);
				}else{//修改
					BeanUtils.copyProperties(dto, vo);
					this.merge(vo);
				}
			}
			return vo;
		} catch (Exception e) {
			e.printStackTrace();
			return vo;
		}
	}

	@Override
	public SaleCardOrderDetail findByOrderNo(String orderNo) {
		if(StringUtil.isEmpty(orderNo)) return null;
		String hql = " from SaleCardOrderDetail t where t.orderNo = ?1";
		List<SaleCardOrderDetail> resultList = this.findByQuery(hql, orderNo);
		if(resultList != null && resultList.size() > 0) {
			return resultList.get(0);
		}
		return null;
	}

	@Override
	public DTOSaleCardOrder findSaleCardOrderByOrderNo(String orderNo) {
		if(StringUtil.isEmpty(orderNo)) return null;
		StringBuilder sb = queryOrderSql();
		sb.append(" from m_base_wxxnk_order t1 left join tbl_sale_card_order_detail t2 on t1.order_no = t2.order_no where t1.order_no = ?1");
		Query query = this.entityManager.createNativeQuery(sb.toString());
		query.setParameter(1, orderNo);
		List<Object[]> resultList = query.getResultList();
		if(resultList != null && resultList.size() > 0) {
			Object[] result = resultList.get(0);
			DTOSaleCardOrder dto = assembleDTOSaleCardOrder(result);
			return dto;
		}
		return null;
	}

	@Override
	public DTOSaleCardOrder findSaleCardOrderBySwiftNumber(String swiftNumber) {
		if(StringUtil.isEmpty(swiftNumber)) return null;
		StringBuilder sb = queryOrderSql();
		sb.append(" from m_base_wxxnk_order t1 left join tbl_sale_card_order_detail t2 on t1.order_no = t2.order_no where t1.swift_number = ?1");
		Query query = this.entityManager.createNativeQuery(sb.toString());
		query.setParameter(1, swiftNumber);
		List<Object[]> resultList = query.getResultList();
		if(resultList != null && resultList.size() > 0) {
			Object[] result = resultList.get(0);
			DTOSaleCardOrder dto = assembleDTOSaleCardOrder(result);
			return dto;
		}
		return null;
	}
	
	private DTOSaleCardOrder assembleDTOSaleCardOrder(Object[] result) {
		DTOSaleCardOrder dto = new DTOSaleCardOrder();
		dto.setId(String.valueOf(result[0]));
		dto.setOrderNo(String.valueOf(result[1]));
		dto.setOrderType(String.valueOf(result[2]));
		dto.setOrderTypeName(String.valueOf(result[3]));
		dto.setAccId(String.valueOf(result[4]));
		dto.setAccName(String.valueOf(result[5]));
		dto.setOrderDetail(String.valueOf(result[6]));
		dto.setOrderStatus(String.valueOf(result[7]));
		dto.setPayment(String.valueOf(result[8]));
		dto.setPayStatus(String.valueOf(result[9]));
		dto.setNeedPay(((BigDecimal)result[10]).longValue());
		dto.setRealPay(((BigDecimal)result[11]).longValue());
		dto.setTradeType(String.valueOf(result[12]));
		dto.setSwiftNumber(String.valueOf(result[13]));
		dto.setCommidityId(String.valueOf(result[14]));
		dto.setCommidityName(String.valueOf(result[15]));
		dto.setCommidityType(String.valueOf(result[16]));
		dto.setCardNo(String.valueOf(result[17]));
		dto.setMrchId(String.valueOf(result[18]));
		dto.setMrchName(String.valueOf(result[19]));
		dto.setMrchType(String.valueOf(result[20]));
		dto.setPsamId(String.valueOf(result[21]));
		dto.setSubId(String.valueOf(result[22]));
		dto.setTradeSno(String.valueOf(result[23]));
		dto.setTradeEndTime(String.valueOf(result[24]));
		dto.setPayEndTime(String.valueOf(result[25]));
		dto.setCancelTime(String.valueOf(result[26]));
		dto.setCreateTime(String.valueOf(result[27]));
		dto.setUpdateTime(String.valueOf(result[28]));
		dto.setIsDeleted(((Boolean)result[29]));
		dto.setRemark(String.valueOf(result[30]));
		dto.setOrderParentType(String.valueOf(result[31]));
		dto.setTermNo(String.valueOf(result[32]));
		dto.setSaleCount(((BigDecimal)result[33]).intValue());
		dto.setDenomination(((BigDecimal)result[34]).longValue());
		return dto;
	}
	
	private DTOSaleCardRecord assembleDTOSaleCardRecord(Object[] result) {
		DTOSaleCardRecord dto = new DTOSaleCardRecord();
		dto.setId(String.valueOf(result[0]));
		dto.setOrderNo(String.valueOf(result[1]));
		dto.setOrderType(String.valueOf(result[2]));
		dto.setOrderTypeName(String.valueOf(result[3]));
		dto.setAccId(String.valueOf(result[4]));
		dto.setAccName(String.valueOf(result[5]));
		dto.setOrderDetail(String.valueOf(result[6]));
		dto.setOrderStatus(String.valueOf(result[7]));
		dto.setPayment(String.valueOf(result[8]));
		dto.setNeedPay(((BigDecimal)result[9]).longValue());
		dto.setRealPay(((BigDecimal)result[10]).longValue());
		dto.setTradeType(String.valueOf(result[11]));
		dto.setSwiftNumber(String.valueOf(result[12]));
		dto.setCommidityId(String.valueOf(result[13]));
		dto.setCommidityName(String.valueOf(result[14]));
		dto.setCommidityType(String.valueOf(result[15]));
		dto.setCardNo(String.valueOf(result[16]));
		dto.setMrchId(String.valueOf(result[17]));
		dto.setMrchName(String.valueOf(result[18]));
		dto.setMrchType(String.valueOf(result[19]));
		dto.setPsamId(String.valueOf(result[20]));
		dto.setSubId(String.valueOf(result[21]));
		dto.setTradeSno(String.valueOf(result[22]));
		dto.setTradeEndTime(String.valueOf(result[23]));
		dto.setPayEndTime(String.valueOf(result[24]));
		dto.setCreateTime(String.valueOf(result[25]));
		dto.setUpdateTime(String.valueOf(result[26]));
		dto.setIsDeleted(((Boolean)result[27]));
		dto.setRemark(String.valueOf(result[28]));
		dto.setOrderParentType(String.valueOf(result[29]));
		dto.setTermNo(String.valueOf(result[30]));
		dto.setSaleCount(((BigDecimal)result[31]).intValue());
		dto.setDenomination(((BigDecimal)result[32]).longValue());
		return dto;
	}

	private StringBuilder queryOrderSql() {
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.id,")
		.append("t1.order_no,")
		.append("t1.order_type,")
		.append("t1.order_type_name,")
		.append("t1.acc_id,")
		.append("t1.acc_name,")
		.append("t1.order_detail,")
		.append("t1.order_status,")
		.append("t1.payment,")
		.append("t1.pay_status,")
		.append("t1.need_pay,")
		.append("t1.real_pay,")
		.append("t1.trade_type,")
		.append("t1.swift_number,")
		.append("t1.commidity_id,")
		.append("t1.commidity_type,")
		.append("t1.commidity_name,")
		.append("t1.card_no,")
		.append("t1.mrch_id,")
		.append("t1.mrch_name,")
		.append("t1.mrch_type,")
		.append("t1.psam_id,")
		.append("t1.sub_id,")
		.append("t1.trade_sno,")
		.append("t1.trade_end_time,")
		.append("t1.pay_end_time,")
		.append("t1.cancel_time,")
		.append("t1.create_time,")
		.append("t1.update_time,")
		.append("t1.creator,")
		.append("t1.updater,")
		.append("t1.is_deleted,")
		.append("t1.remark,")
		.append("t1.order_parent_type,")
		.append("t1.term_no,")
		.append("t2.sale_count,")
		.append("t2.denomination");
		return sb;
	}
	
	private StringBuilder queryRecordSql() {
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.id,")
		.append("t1.order_no,")
		.append("t1.order_type,")
		.append("t1.order_type_name,")
		.append("t1.acc_id,")
		.append("t1.acc_name,")
		.append("t1.order_detail,")
		.append("t1.order_status,")
		.append("t1.payment,")
		.append("t1.need_pay,")
		.append("t1.real_pay,")
		.append("t1.trade_type,")
		.append("t1.swift_number,")
		.append("t1.commidity_id,")
		.append("t1.commidity_type,")
		.append("t1.commidity_name,")
		.append("t1.card_no,")
		.append("t1.mrch_id,")
		.append("t1.mrch_name,")
		.append("t1.mrch_type,")
		.append("t1.psam_id,")
		.append("t1.sub_id,")
		.append("t1.trade_sno,")
		.append("t1.trade_end_time,")
		.append("t1.pay_end_time,")
		.append("t1.create_time,")
		.append("t1.update_time,")
		.append("t1.creator,")
		.append("t1.updater,")
		.append("t1.is_deleted,")
		.append("t1.remark,")
		.append("t1.order_parent_type,")
		.append("t1.term_no,")
		.append("t2.sale_count,")
		.append("t2.denomination");
		return sb;
	}

	@Override
	public DTOSaleCardRecord findSaleCardRecordBySwiftNumber(String swiftNumber) {
		if(StringUtil.isEmpty(swiftNumber)) return null;
		StringBuilder sb = queryRecordSql();
		sb.append(" from m_base_wxxnk_trade_record t1 left join tbl_sale_card_order_detail t2 on t1.order_no = t2.order_no where t1.swift_number = ?1");
		Query query = this.entityManager.createNativeQuery(sb.toString());
		query.setParameter(1, swiftNumber);
		List<Object[]> resultList = query.getResultList();
		if(resultList != null && resultList.size() > 0) {
			Object[] result = resultList.get(0);
			DTOSaleCardRecord dto = assembleDTOSaleCardRecord(result);
			return dto;
		}
		return null;
	}

}
